/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.dummydevice.modules.obstacles;

import net.zarubsys.robotplayground.core.Environment;
import net.zarubsys.robotplayground.core.device.NullDevice;
import net.zarubsys.robotplayground.core.modules.AbstractModule;
import net.zarubsys.robotplayground.core.modules.annotations.Function;
import net.zarubsys.robotplayground.core.modules.annotations.Param;

/**
 * DummyObstaclesModule
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class DummyObstaclesModule extends AbstractModule<NullDevice, DummyObstaclesModuleConfiguration> {

	public static final String ID = "dummyObstacles";
	
	private MapContainer map;

	@Override
	public void init(NullDevice device, DummyObstaclesModuleConfiguration configuration, Environment env) {
		this.map = DummyObstaclesModuleConfiguration.loadMapFromFile(configuration.getFile());
	}
	
	@Function(description = "Detects whether there is an obstacle in the distance of 1 from the given point in the given direction. " +
			"Throws exception when given position is on the edge of the map. Retrurns true if on the point is the edge of the map.")
	public boolean isObstacleAhead(@Param(name = "x") Integer x, @Param(name = "y") Integer y, @Param(name = "direction") Integer direction) {
		validateCoordinates(x, y);
		
		double radians = Math.toRadians(direction);
		int dx = (int) Math.round(Math.sin(radians));
		int dy = (int) Math.round(Math.cos(radians));
		x += dx;
		y -= dy;
		
		if (isOnTheEdge(x, y)) return true;
		return map.getContent().get(y).get(x);
	}

	private void validateCoordinates(int x, int y) {
		int ySize = map.getContent().size();
		int xSize = map.getLineLength();
		if (y == 0 || ySize <= (y - 1)) {
			throw new IllegalArgumentException("y = " + y + " is out of map. Map size is " + xSize + " : " + ySize);
		}
		if (x == 0 || xSize <= (x - 1)) {
			throw new IllegalArgumentException("x = " + x + " is out of map. Map size is " + xSize + " : " + ySize);
		}
	}
	
	public boolean isOnTheEdge(int x, int y) {
		return (y == 0 || map.getContent().size() <= (y - 1)) || (x == 0 || map.getLineLength() <= (x - 1));
	}
	
	public boolean isOnTheObstacle(int x, int y) {
		validateCoordinates(x, y);
		return map.getContent().get(y).get(x);
	}
	
	public MapContainer getMap() {
		return map;
	}

	@Override
	public void close() {
		// ignore
	}

	@Override
	public String getId() {
		return ID;
	}
	
	@Override
	public String getRequiredDevice() {
		return NullDevice.ID;
	}

}

